﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Script.Serialization;

namespace MvcControlsToolkit
{
    public class DatePicker: Control
    {
        public string DateFormat
        {
            get { return GetString("dateFormat"); }
            set { SetString("dateFormat", value); }
        }

        public string AppendText
        {
            get { return GetString("appendText"); }
            set { SetString("appendText", value); }
        }

        public bool IsAutoSize
        {
            get
            {
                if (Attributes.ContainsKey("autoSize"))
                    return bool.Parse(Attributes["autoSize"]);
                else return false;
            }
            set { Attributes["autoSize"] = value.ToString().ToLower(); }
        }

        public string ImageUrl
        {
            get { return GetString("buttonImage"); }
            set { SetString("buttonImage", value); }
        }

        public bool IsOnlyButtonImage
        {
            get
            {
                if (Attributes.ContainsKey("buttonImageOnly"))
                    return bool.Parse(Attributes["buttonImageOnly"]);
                else return false;
            }
            set { Attributes["buttonImageOnly"] = value.ToString().ToLower(); }
        }

        public string ButtonText
        {
            get { return GetString("buttonText"); }
            set { SetString("buttonText", value); }
        }

        public bool IsChangeMonth
        {
            get
            {
                if (Attributes.ContainsKey("changeMonth"))
                    return bool.Parse(Attributes["changeMonth"]);
                else return false;
            }
            set { Attributes["changeMonth"] = value.ToString().ToLower(); }
        }

        public bool IsChangeYear
        {
            get
            {
                if (Attributes.ContainsKey("changeYear"))
                    return bool.Parse(Attributes["changeYear"]);
                else return false;
            }
            set { Attributes["changeYear"] = value.ToString().ToLower(); }
        }

        public string CloseText
        {
            get { return GetString("closeText"); }
            set { SetString("closeText", value); }
        }

        public string CurrentText
        {
            get { return GetString("currentText"); }
            set { SetString("currentText", value); }
        }

        public string[] DayNames
        {
            get
            {
                if (Attributes.ContainsKey("dayNames"))
                {
                    var json = new JavaScriptSerializer();
                    var result = json.Deserialize<string[]>(Attributes["dayNames"]);
                    return result;
                }
                else return null;
            }
            set
            {
                var json = new JavaScriptSerializer();
                var result = json.Serialize(value);
                Attributes["dayNames"] = result.ToLower();
            }
        }

        public string[] DayNamesMin
        {
            get
            {
                if (Attributes.ContainsKey("dayNamesMin"))
                {
                    var json = new JavaScriptSerializer();
                    var result = json.Deserialize<string[]>(Attributes["dayNamesMin"]);
                    return result;
                }
                else return null;
            }
            set
            {
                var json = new JavaScriptSerializer();
                var result = json.Serialize(value);
                Attributes["dayNamesMin"] = result.ToLower();
            }
        }

        public string[] DayNamesShort
        {
            get
            {
                if (Attributes.ContainsKey("dayNamesShort"))
                {
                    var json = new JavaScriptSerializer();
                    var result = json.Deserialize<string[]>(Attributes["dayNamesShort"]);
                    return result;
                }
                else return null;
            }
            set
            {
                var json = new JavaScriptSerializer();
                var result = json.Serialize(value);
                Attributes["dayNamesShort"] = result.ToLower();
            }
        }

        public bool IsMondayFirstDay
        {
            get
            {
                if (Attributes.ContainsKey("firstDay"))
                    return bool.Parse(Attributes["firstDay"]);
                else return false;
            }
            set { Attributes["firstDay"] = Convert.ToInt16(value.ToString().ToLower()).ToString(); }
        }

        public bool IsCurrentDayToday
        {
            get
            {
                if (Attributes.ContainsKey("gotoCurrent"))
                    return bool.Parse(Attributes["gotoCurrent"]);
                else return false;
            }
            set { Attributes["gotoCurrent"] = value.ToString().ToLower(); }
        }

        public bool IsRtl
        {
            get
            {
                if (Attributes.ContainsKey("isRTL"))
                    return bool.Parse(Attributes["isRTL"]);
                else return false;
            }
            set { Attributes["isRTL"] = value.ToString().ToLower(); }
        }

        public DateTime? MaxDate
        {
            get
            {
                if (Attributes.ContainsKey("maxDate"))
                    return DateTime.Parse(Attributes["maxDate"]);
                else return null;
            }
            set { Attributes["maxDate"] = String.Format("new Date({0}, {1}, {2})", value.Value.Date.Year, value.Value.Date.Day, value.Value.Date.Month); }
        }

        public DateTime? MinDate
        {
            get
            {
                if (Attributes.ContainsKey("minDate"))
                    return DateTime.Parse(Attributes["minDate"]);
                else return null;
            }
            set { Attributes["minDate"] = String.Format("new Date({0}, {1}, {2})", value.Value.Date.Year, value.Value.Date.Day, value.Value.Date.Month); }
        }

        public string[] MonthNames
        {
            get
            {
                if (Attributes.ContainsKey("monthNames"))
                {
                    var json = new JavaScriptSerializer();
                    var result = json.Deserialize<string[]>(Attributes["monthNames"]);
                    return result;
                }
                else return null;
            }
            set
            {
                var json = new JavaScriptSerializer();
                var result = json.Serialize(value);
                Attributes["monthNames"] = result.ToLower();
            }
        }

        public string[] MonthNamesShort
        {
            get
            {
                if (Attributes.ContainsKey("monthNamesShort"))
                {
                    var json = new JavaScriptSerializer();
                    var result = json.Deserialize<string[]>(Attributes["monthNamesShort"]);
                    return result;
                }
                else return null;
            }
            set
            {
                var json = new JavaScriptSerializer();
                var result = json.Serialize(value);
                Attributes["monthNamesShort"] = result.ToLower();
            }
        }

        public bool IsNavigationAsDateFormat
        {
            get
            {
                if (Attributes.ContainsKey("navigationAsDateFormat"))
                    return bool.Parse(Attributes["navigationAsDateFormat"]);
                else return false;
            }
            set { Attributes["navigationAsDateFormat"] = value.ToString().ToLower(); }
        }

        public string NextText
        {
            get { return GetString("nextText"); }
            set { SetString("nextText", value); }
        }

        public int NumberOfMonths
        {
            get
            {
                if (Attributes.ContainsKey("numberOfMonths"))
                    return int.Parse(Attributes["numberOfMonths"]);
                else return 1;
            }
            set { Attributes["numberOfMonths"] = value.ToString().ToLower(); }
        }

        public string PrevText
        {
            get { return GetString("prevText"); }
            set { SetString("prevText", value); }
        }

        public bool IsSelectOtherMonths
        {
            get
            {
                if (Attributes.ContainsKey("selectOtherMonths"))
                    return bool.Parse(Attributes["selectOtherMonths"]);
                else return false;
            }
            set { Attributes["selectOtherMonths"] = value.ToString().ToLower(); }
        }

        public int ShortYearCutoff
        {
            get
            {
                if (Attributes.ContainsKey("shortYearCutoff"))
                    return int.Parse(Attributes["shortYearCutoff"]);
                else return 1;
            }
            set { Attributes["shortYearCutoff"] = value.ToString().ToLower(); }
        }

        public string ShowEffect
        {
            get { return GetString("showAnim"); }
            set { SetString("showAnim", value); }
        }

        public bool IsShowButtonPanel
        {
            get
            {
                if (Attributes.ContainsKey("showButtonPanel"))
                    return bool.Parse(Attributes["showButtonPanel"]);
                else return false;
            }
            set { Attributes["showButtonPanel"] = value.ToString().ToLower(); }
        }

        public int ShowCurrentAtPos
        {
            get
            {
                if (Attributes.ContainsKey("showCurrentAtPos"))
                    return int.Parse(Attributes["showCurrentAtPos"]);
                else return 0;
            }
            set { Attributes["showCurrentAtPos"] = value.ToString().ToLower(); }
        }

        public bool IsShowMonthAfterYear
        {
            get
            {
                if (Attributes.ContainsKey("showMonthAfterYear"))
                    return bool.Parse(Attributes["showMonthAfterYear"]);
                else return false;
            }
            set { Attributes["showMonthAfterYear"] = value.ToString().ToLower(); }
        }

        public bool IsShowOtherMonth
        {
            get
            {
                if (Attributes.ContainsKey("showOtherMonths"))
                    return bool.Parse(Attributes["showOtherMonths"]);
                else return false;
            }
            set { Attributes["showOtherMonths"] = value.ToString().ToLower(); }
        }

        public bool IsShowWeek
        {
            get
            {
                if (Attributes.ContainsKey("showWeek"))
                    return bool.Parse(Attributes["showWeek"]);
                else return false;
            }
            set { Attributes["showWeek"] = value.ToString().ToLower(); }
        }

        public string ShowOn
        {
            get { return GetString("showOn"); }
            set { SetString("showOn", value); }
        }

        public int StepMonths
        {
            get
            {
                if (Attributes.ContainsKey("stepMonths"))
                    return int.Parse(Attributes["stepMonths"]);
                else return 0;
            }
            set { Attributes["stepMonths"] = value.ToString().ToLower(); }
        }

        public string WeekHeader
        {
            get { return GetString("weekHeader"); }
            set { SetString("weekHeader", value); }
        }

        public string YearRange
        {
            get { return GetString("yearRange"); }
            set { SetString("yearRange", value); }
        }

        public string YearSuffix
        {
            get { return GetString("yearSuffix"); }
            set { SetString("yearSuffix", value); }
        }

        public string OnCreate
        {
            get { return Attributes["create"]; }
            set { Attributes["create"] = value; }
        }

        public string OnBeforeShow
        {
            get { return Attributes["beforeShow"]; }
            set { Attributes["beforeShow"] = value; }
        }

        public string OnBeforeShowDay
        {
            get { return Attributes["beforeShowDay"]; }
            set { Attributes["beforeShowDay"] = value; }
        }

        public string OnChangeMonthYear
        {
            get { return Attributes["onChangeMonthYear"]; }
            set { Attributes["onChangeMonthYear"] = value; }
        }

        public string OnClose
        {
            get { return Attributes["onClose"]; }
            set { Attributes["onClose"] = value; }
        }

        public string OnSelect
        {
            get { return Attributes["onSelect"]; }
            set { Attributes["onSelect"] = value; }
        }

        public DatePicker(string name) : base(name)
        {
        }

        public override string GetJScript()
        {
            var result = new StringBuilder();

            foreach (string key in Attributes.Keys)
            {
                result.AppendFormat(", {0}: {1}", key, Attributes[key]);
            }

            if (result.Length > 0)
                result.Remove(0, 1);

            result.Append("});");

            result.Insert(0, "var " + Name + " = $(\"#" + Name + "\").datepicker({");

            return result.ToString();
        }

        public override string GetHtml()
        {
            return String.Format("<input type=\"text\" id=\"{0}\">", Name);
        }
    }
}
